﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;

public partial class LichSuGiaoDich : System.Web.UI.Page
{
    DataProvider duLieu = new DataProvider();
    ListBox listBox = new ListBox();

    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty((string)Session["Username"]) & (int)Session["Logged"] == 1)
            {
                Panelfull.Visible = true;
            }
            else
            {
                Panelfull.Visible = false;
                showthongbao0.Text = "<a href='Default.aspx'>BẠN HÃY ĐĂNG NHẬP ĐỂ SỬ DỤNG ĐƯỢC CHỨC NĂNG NÀY.</a>";
                return;
            }
            //=== CHI TIẾT ĐỊA CHỈ ====

            if (Request.QueryString["xemdiachi"] != null)
            {
                Paneldiachichitiet.Visible = true;
                int id = int.Parse(Request.QueryString["xemdiachi"]);
                listBox.Items.Clear();
                duLieu.MakeDuLieu(listBox, (string)Session["ID"]);
                duLieu.MakeDuLieu(listBox, id.ToString());

                DataTable bangchitietdiachi = duLieu.Load_Du_Lieu_New("SELECT diachinn from DONDATHANG where makh=? and maddh=?", listBox);

                if (bangchitietdiachi.Rows.Count != 0)
                {
                    diachichitiet.Text = (string)bangchitietdiachi.Rows[0][0];
                }
                else
                {
                    Paneldiachichitiet.Visible = false;
                }
            }
            else
            {
                Paneldiachichitiet.Visible = false;
            }

            //=== CHI TIẾT ĐƠN ĐẶT HÀNG ====

            if (Request.QueryString["id"] != null && Request.QueryString["man"] == "xem")
            {
                Panelchitietddh.Visible = true;
                int id = int.Parse(Request.QueryString["id"]);

                listBox.Items.Clear();
                duLieu.MakeDuLieu(listBox, (string)Session["ID"]);
                duLieu.MakeDuLieu(listBox, id.ToString());

                DataTable bangchitietddh = duLieu.Load_Du_Lieu_New("SELECT CHITIETDDHANG.MADDH, CHITIETDDHANG.MAHH, HANGHOA.TENHH, CHITIETDDHANG.DONGIABAN, CHITIETDDHANG.SLDAT FROM CHITIETDDHANG INNER JOIN  HANGHOA ON CHITIETDDHANG.MAHH = HANGHOA.MAHH INNER JOIN DONDATHANG ON CHITIETDDHANG.MADDH = DONDATHANG.MADDH where makh=? and DONDATHANG.maddh=?", listBox);

                showthongbao3.Text = "";
                if (bangchitietddh.Rows.Count != 0)
                {
                    xemchitiet.DataSource = bangchitietddh;
                    xemchitiet.DataBind();
                }
                else
                {
                    showthongbao3.Text = "<font color='red'>Đơn Hàng Này Không Có Món Hàng Nào.</font>";
                }
            }
            else
            {
                Panelchitietddh.Visible = false;
            }

            //==== Sửa Đơn ĐẶT HÀNG ===

            if (Request.QueryString["suaddh"] != null)
            {
                int maddh = int.Parse(Request.QueryString["suaddh"]);
                listBox.Items.Clear();
                duLieu.MakeDuLieu(listBox, (string)Session["ID"]);
                duLieu.MakeDuLieu(listBox, maddh.ToString());

                DataTable bangchitietddh = duLieu.Load_Du_Lieu_New("SELECT DONDATHANG.MAKH, CHITIETDDHANG.* FROM CHITIETDDHANG INNER JOIN DONDATHANG ON CHITIETDDHANG.MADDH = DONDATHANG.MADDH where DONDATHANG.makh=? and CHITIETDDHANG.maddh=?", listBox);

                if (bangchitietddh.Rows.Count != 0)
                {
                    showthongbaoz.Text = "<font color='red'>BẠN CÓ CHẮC CHẮN MUỐN SỬA KHÔNG</font>";
                    Panelfull.Visible = false;
                    Panelfull0.Visible = true;
                }
            }
            loadbangchuagiaohang();
        }
    }

    public void loadbangchuagiaohang()
    {
        DataTable bangchude = null;
        listBox.Items.Clear();
        duLieu.MakeDuLieu(listBox, (string)Session["ID"]);

        bangchude = duLieu.Load_Du_Lieu_New("select * from DONDATHANG where makh=? and dagiao=0 order by maddh DESC", listBox);
        if (bangchude.Rows.Count != 0)
        {
            bangchuagiaohang.DataSource = bangchude;
            bangchuagiaohang.DataBind();
        }
        else
        {
            showthongbao1.Text = "<font color='red'>Bạn Không Có Đơn Hàng Nào Chưa Giao.</font>";
        }
    }

    public void loadbangdagiaohang()
    {
        DataTable bangchude = null;

        listBox.Items.Clear();
        duLieu.MakeDuLieu(listBox, (string)Session["ID"]);

        bangchude = duLieu.Load_Du_Lieu_New("select * from DONDATHANG where makh=? and dagiao=1 order by maddh DESC", listBox);
        if (bangchude.Rows.Count != 0)
        {
            bangdagiaohang.DataSource = bangchude;
            bangdagiaohang.DataBind();
        }
        else
        {
            showthongbao2.Text = "<font color='red'>Bạn Không Có Đơn Hàng Nào Đã Giao.</font>";
        }
    }

    protected void chonkieu_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        Panelchitietddh.Visible = false;
        Paneldiachichitiet.Visible = false;

        if (int.Parse(chonkieu.SelectedValue) == 1)
        {
            Panelchuagiaohang.Visible = true;
            Paneldagiaohang.Visible = false;
            loadbangchuagiaohang();
        }
        else
        {
            Paneldagiaohang.Visible = true;
            loadbangdagiaohang();
            Panelchuagiaohang.Visible = false;
        }
    }

   
    protected void tienhanhsua_Click(object sender, System.EventArgs e)
    {
        int id = int.Parse(Request.QueryString["suaddh"]);

        listBox.Items.Clear();
        duLieu.MakeDuLieu(listBox, (string)Session["ID"]);
        duLieu.MakeDuLieu(listBox, id.ToString());

        DataTable bangchitietddh = duLieu.Load_Du_Lieu_New("SELECT CHITIETDDHANG.MAHH AS ID, CHITIETDDHANG.SLDAT as So_Luong, HANGHOA.TENHH AS Ten_SP, CHITIETDDHANG.DONGIABAN as Don_Gia FROM CHITIETDDHANG INNER JOIN  HANGHOA ON CHITIETDDHANG.MAHH = HANGHOA.MAHH INNER JOIN DONDATHANG ON CHITIETDDHANG.MADDH = DONDATHANG.MADDH where makh=? and DONDATHANG.maddh=?", listBox);
        showthongbaoz.Text = "";
        if (bangchitietddh.Rows.Count != 0)
        {
            Session["Gio_Hang"] = bangchitietddh;

            Session["TrangThaiSuaDDHang"] = 1;
            Session["IDDDHangChuanBiSua"] = id;

            Response.Redirect("Gio_Hang.aspx");
        }
        else
        {
            showthongbaoz.Text = "<font color='red'>Đơn Hàng Này Không Có Món Hàng Nào.</font>";
        }
    }

    protected void khongsua_Click(object sender, System.EventArgs e)
    {
        Response.Redirect("LichSuGiaoDich.aspx");
    }
}